package com.jffccloud.auth.web;

import com.jffccloud.auth.service.UserService;
import com.jffccloud.common.model.LoginAuthDto;
import com.jffccloud.common.web.BaseController;
import com.jffccloud.common.web.wrapper.WrapMapper;
import com.jffccloud.common.web.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("index")
@Api(value = "mainController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MainController extends BaseController {
    @Autowired
    UserService userService;
    @RequestMapping("/menu")
    public Wrapper<List> list(HttpServletResponse response, HttpServletRequest request) {
        LoginAuthDto principal = getLoginAuthDto();
        List list = userService.getMenu(principal.getUserId());
        if (list == null) {
            return WrapMapper.error("无法获取数据");
        } else {
            for(int i=0;i<list.size();i++){
                Map map = (Map)list.get(i);
                if(map.get("url")!=null){
                    if(request.getSession().getAttribute("REPORT_SESSION_ID")!=null) {
                        String sessionId = (String) request.getSession().getAttribute("REPORT_SESSION_ID");
                        map.put("url",map.get("url"+"&sessionId="+sessionId));
                    }
                }
            }
            return WrapMapper.ok(list);
        }
    }
}
